// pages/contract_mag/loading.js
import { baseUrl } from '../../utils/http.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    markNum: 0,
    showResult: false,
    // 结果页参数
    contractNum: false,
    contractNo: '',
    templateId: '',
    pendingContracts: [],
    reviewUrl: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log('h5服务：', this.serviceUrl)
    // 处理上上签域名
    let url = '';
    if (baseUrl === 'https://recruit-dev.lgong.com/') {
      url = `https://wx101.bestsign.info`
    } else if (baseUrl === 'https://recruit-test.lgong.com/') {
      url = `https://wx101.bestsign.info`
    } else if (baseUrl === 'https://recruit.lgong.com/') {
      url = `https://wx100.bestsign.cn:443`
    }
    this.setData({
      reviewUrl: url,
    })

    this.checkSignStatus()

  },

  // 查询签署结果
  checkSignStatus() {
    let data = {
      "contractNo": wx.getStorageSync('CONTRACTNO') || '',
    };
    let that = this;
    getApp().$http.checkSignStatus(data).then(res => {
      let result = res
      //   console.log('签署中间页前面呢~~',res)
      // wx.showLoading({
      //   title: '签署中，请稍候'
      // })
      if (result.status == 2) {
        wx.hideLoading()
        that.setData({
          showResult: true,
        })
        getApp().$http.getContractList().then(res => {
          that.setData({
            pendingContracts: res.pendingContracts,
          })
        })
      } else if (result.status == 4) {
        // console.log('签署进行中~~')
        that.setData({
          markNum: that.data.markNum++,
        })
        // console.log(this.markNum)
        if (that.data.markNum < 4) {
          setTimeout(checkSignStatus, 1000)
        } else {
          clearTimeout(checkSignStatus)
        }
      } else {
        setTimeout(() => {
          getApp().$routes.replaceTo(getApp().$routes.contract_index)
        }, 2000);
      }
    })
  },

  // 继续签约或返回合同列表
  signBtn() {
    let that = this
    if (this.data.pendingContracts.length > 0) {
      wx.showLoading({
        title: '跳转中，请稍候'
      })
      let item = that.pendingContracts[0];
      getApp().$http.singContract(item, function complated(res) {
        wx.hideLoading()
        wx.setStorageSync('CONTRACTNO', item.contractNo);
        let signingLink = params.signingLink
        signingLink = signingLink.replace(/^https:\/\/[^/]+/, that.data.reviewUrl)
        wx.setStorageSync('URL', signingLink);
        getApp().$routes.goTo(getApp().$routes.contract_sign)//去签署
      })

    } else {
      getApp().$routes.replaceTo(getApp().$routes.contract_index)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})